// $Id: loggercountfilestore.cpp 33 2004-09-22 12:13:27Z hansemil $
#include "loggercountfilestore.h"
#include "loghandler.h"
#include "node.h"
#include <iostream>
#include <list>
#include <vector>
using namespace std;

/**
 * @todo Write documentation
 */
LoggerCountFileStore::LoggerCountFileStore(){
	LogHandler::getInstance()->addLogger(this, "clock_tick");
}

/**
 * Opens file for writing and stores the Eos pointer
 */
void LoggerCountFileStore::eos_simulate_start(void *eos)
{
	openFile(&m_fout, "countfilestore.csv");
	m_fout << "Time Step, Average, Minimum, First Quartile, Median, Third Quartile, Maximum, Number of Nodes" << endl;

	m_eos = (Eos *)eos;
}

/**
 * Closes file
 */
void LoggerCountFileStore::eos_simulate_stop(void *eos)
{
	m_fout.close();
}

/**
 * Gathers the statistics for each tick
 */
void LoggerCountFileStore::clock_tick(unsigned int time)
{
	int currentSize = 0, sumSize = 0;
	std::vector<int> sizeList;

	// Write time step
	m_fout << time;

	// Get data
	std::list<Node *> *nodes = m_eos->getNodes();
	for (std::list<Node *>::iterator node=nodes->begin(); node!=nodes->end(); node++) {
		currentSize = (*node)->getFileStoreSize();
		sumSize += currentSize;
		sizeList.push_back(currentSize);
	}

	// Write statistics
	writeCountStats(m_fout, sizeList, sumSize);

	// Write end
	m_fout << endl;
}
